using UnityEngine;

public class CharacterBalance : MonoBehaviour
{
	[SerializeField]
	private CharacterInformation info;

	[SerializeField]
	private Rigidbody leftKnee;

	[SerializeField]
	private Rigidbody rightKnee;

	[SerializeField]
	private Rigidbody leftLeg;

	[SerializeField]
	private Rigidbody rightLeg;

	public float balanceForce = 1f;

	private void Update()
	{
		if (info.isFallen > 0f && info.isGrounded)
		{
			StayBalanced();
		}
	}

	private void StayBalanced()
	{
		if ((bool)rightLeg || (bool)leftLeg)
		{
			Rigidbody rigidbody = rightKnee;
			if (Vector3.Angle(leftLeg.transform.up, Vector3.up) < Vector3.Angle(rightLeg.transform.up, Vector3.up))
			{
				rigidbody = leftKnee;
			}
			Vector3 a = leftLeg.transform.up + rightLeg.transform.up;
			a.y = 0f;
			float d = 1f;
			rigidbody.AddForce(a * 10000f * balanceForce * d * Time.deltaTime, ForceMode.Acceleration);
		}
	}
}
